<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class User_group extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();

		$this->load->model('user_group_model', 'group_model');
	}

	/**
	 * 用户组列表显示
	 */
	public function index()
	{
		$type = $this->input->get('type', TRUE);
		if (!$type)
		{
			$type = 'member';
		}

		$this->data['type'] = $type;
		$this->data['groups'] = $this->group_model->get_all();
		$this->load->view('admin/user_group_index', $this->data);
	}

	/**
	 * 更新和添用户组（提交）
	 */
	public function saves()
	{
		$type = $this->input->post('type', TRUE);
		$names = $this->input->post('name', TRUE);
		//$vieworders = $this->input->post('vieworder');
		$new_names = $this->input->post('new_name', TRUE);
		//$new_vieworders = $this->input->post('new_vieworder');

		//update
		if ($names)
		{
			foreach ($names as $groupid => $name)
			{
				$update['groupid'] = intval($groupid);
				$update['name'] = $name;
				//$update['vieworder'] = intval($vieworders[$groupid]);

				$this->group_model->update($update);
			}
		}

		//insert
		if ($new_names)
		{
			foreach ($new_names as $k => $new_name)
			{
				if (!$new_name) continue;

				$insert['type'] = $type;
				$insert['name'] = $new_name;
				//$insert['vieworder'] = intval($new_vieworders[$k]);

				$this->group_model->insert($insert);
			}
		}

		redirect('admin/user_group/index?type=' . $type);
	}

	/**
	 * 删除用户组
	 */
	public function del()
	{
		$groupid = $this->input->get('groupid', TRUE);

		if ($groupid <= 7)
		{
			show_admin_msg('请勿删除系统默认用户组');
		}

		$group = $this->group_model->get($groupid);

		if ($group)
		{
			//添加:如果该经纪人用户组存在用户，则不允许删除的判断
			$this->group_model->delete($groupid);
			show_admin_msg('用户组删除成功', 'success');
		}
		else
		{
			show_admin_msg('用户组不存在或已删除');
			//redirect();
		}
	}

	/**
	 * 编辑用户组权限显示页面
	 */
	public function right()
	{
		$this->load->helper('form');

		$groupid = $this->input->get('groupid', TRUE);

		$this->data['group'] = $this->group_model->get($groupid);
		$this->data['right'] = $this->group_model->get_right($groupid);
		//var_dump(unserialize($this->data['group_right']['allow_post_type']));exit;
		$this->data['groupid'] = $groupid;
		$this->load->view('admin/user_group_right', $this->data);
	}

	/**
	 * 更新用户组权限操作（提交）
	 */
	public function doright()
	{
		$groupid = $this->input->post('groupid', TRUE);
		$right = $this->input->post('right', TRUE);

		$this->group_model->replace_right($right);

		redirect('admin/user_group/right?groupid=' . $groupid);
	}
}

/* End of file user_group.php */
/* Location: ./application/controllers/admin/user_group.php */
